import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import MainPage from '@/page/MainPage.vue'
import LoginPage from '@/page/LoginPage.vue'
import MineDetail from "@/page/MineDetail.vue";
import FaqPage from "@/page/FaqPage.vue";
import RegisterPage from "@/page/RegisterPage.vue";
import Person from '@/page/PersonReal/Person.vue';
import PersonSelectLocation from '@/page/PersonReal/page/SelectLocation.vue'
import PersonUpload from '@/page/PersonReal/page/PersonUpload.vue';
import SelectCard from '@/page/PersonReal/page/SelectCard.vue';
import UserInfo from '@/page/PersonReal/page/UserInfo.vue';

Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/', name: 'home', component: MainPage,
    },
    { path: '/faq', name: 'faq', component: FaqPage },
    {
      path: '/user/mine', name: 'mine', meta: 'auth', component: MineDetail,
    },
    {
      path: '/login', name: 'login', component: LoginPage, meta: 'notAuth'
    },
    {
      path: '/register', name: 'register', component: RegisterPage,
    },
    {
      path: '/person', name: 'person', component: Person, meta: 'auth',
      children: [
        {
          path: '/person/upload_card',
          name: 'uploadCard',
          component: PersonUpload
        },
        {
          path: '/person/select_location',
          name: 'selectLocation',
          component: PersonSelectLocation,
        },
        {
          path: '/person/upload_card/select_card/:card',
          name: 'selectCard',
          component: SelectCard,
        },
        {
          path: '/person/user_info',
          name: 'userInfo',
          component: UserInfo,
        }
      ],
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  const isLogin = await store.getters.isLogin
  if (to.meta === 'auth') {
    if (isLogin) {
      if (to.fullPath === '/person' && from.fullPath.match(/^\/person\/.*/)) {
        next('/')
      } else {
        next()
      }
    } else {
      next('/login?redirect=' + to.fullPath)
    }
  } else if (to.meta === 'notAuth') {
    if (isLogin) {
      next('/')
    } else {
      next()
    }
  } else {
    next()
  }

})

export default router
